package westh.ilib.struts.action.manage.resource;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.mule.extras.client.MuleClient;
import org.mule.umo.UMOMessage;

import westh.util.YyAction;
import westh.ilib.bean.manage.ResourceBean;

public class ResourceListPreAction extends YyAction {
	// 定义commons log
	private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
			.getLog(ResourceListPreAction.class);

	public ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// 日志和权限检查，请勿删除
		westh.log.YyLog.addFootprint(request, "动作-资源信息列表");
		/**
		 * ****************in data start********************************************
		 */
		// 查询数据文件
		String dbName = "resource.dbxml";
		String methodName = "getResList";
		JSONObject inJson = new JSONObject();
		//data in
		JSONObject inStr = new JSONObject();
		inStr.put("dbName", dbName);
		//
		inJson.put("methodName", methodName);
		inJson.put("jsonStr", inStr);
//		inJson.put("dbName", dbName);
		String inputStr = inJson.toString();
		log.debug("inputStr="+inputStr);
		/**
		 * ****************in data end********************************************
		 */
		String outString = "";
		try {
			MuleClient clientD = new MuleClient();
			// 调用WebService
			UMOMessage umo = clientD.send("axis:http://localhost:4381/LIBJson/iDlibJson?method=getResList",inStr.toString(), null);
			//UMOMessage umo = clientD.send("axis:http://localhost:4381/LIBJson/iDlibJson?method=rulesControl",inputStr, null);
			outString = (String) umo.getPayload();
			log.debug(" outString="+outString);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		// 解析outString
		JSONObject picOutStr = JSONObject.fromObject(outString);
		String retCode = picOutStr.getString("retCode");
		List list = new ArrayList();
		ResourceBean resource = new ResourceBean();
		// 判断
		if (retCode.equals("0100")) {
			String result = picOutStr.getString("result");
			JSONArray resultArry = JSONArray.fromObject(result);
			int size = resultArry.size();
			for (int i = 0; i < size; i++) {
				resource = new ResourceBean();
				JSONObject jsonobj = JSONObject.fromObject(resultArry.get(i));
				resource.setResId(jsonobj.getString("rscId"));
				resource.setResTitle(jsonobj.getString("rscTitle"));
				resource.setResSummary(jsonobj.getString("rscAbstract"));
				resource.setResPubDate(jsonobj.getString("rscPubDate"));
				resource.setResAuthor(jsonobj.getString("rscFirstAuthorId"));
				resource.setResOtherAuthor(jsonobj.getString("rscOtherAuthorsId"));
				resource.setResType(jsonobj.getString("rscType"));
				resource.setResField(jsonobj.getString("rscField"));
				resource.setResUrl(jsonobj.getString("rscUrl"));
//				resource.setCdoiId(jsonobj.getString("resIdentifier"));
				list.add(resource);
			}
		}
		request.setAttribute("list", list);
		return mapping.findForward("succ");
	}
}
